package com.leifeng.demo.WindowDemo.controller;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import com.leifeng.demo.WindowDemo.main.StageManager;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

public class SecondController implements Initializable {
	
    @FXML
    private Button btnCloseThis;

    @FXML
    private Button btnOpenThrid;
    
    //普通的字符串,不过这里用于将该数据发送到第一个窗口  
    private String tranDataToIndex;

    @FXML
    void openThrid(ActionEvent event) throws IOException {
		Stage stage = new Stage();
		Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("view/Third.fxml"));
		stage.setTitle("第三个窗口");
		stage.setResizable(false);
		stage.setScene(new Scene(root));
		stage.initOwner(btnOpenThrid.getScene().getWindow());
		stage.show();
		//将第二个窗口保存到map中
		StageManager.STAGE.put("third", stage);
		StageManager.CONTROLLER.put("secondController", this);
    }

    @FXML
    void closeThis(ActionEvent event) {
        if (this.tranDataToIndex!=null) {  
            FirstController index=(FirstController) StageManager.CONTROLLER.get("firstController");  
            this.tranDataToIndex="第三个窗口";  
            index.txtData.setText("来自second:"+this.tranDataToIndex);  
        }  
        StageManager.STAGE.get("second").close();  
        //删除map中的引用  
        StageManager.STAGE.remove("second");  
        StageManager.CONTROLLER.remove("firstController");  
    }

	@Override
	public void initialize(URL location, ResourceBundle resources) {

	}
	
	//-----------get/set----------------
	public String getTranDataToIndex() {
		return tranDataToIndex;
	}
	
	public void setTranDataToIndex(String tranDataToIndex) {
		this.tranDataToIndex = tranDataToIndex;
	}
}
